/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie06.rmi;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author sylvain
 */
public class Database {

    private String dbURL = "";
    private String user = "";
    private String password = "";
    private java.sql.Connection dbConnect = null;
    private java.sql.Statement dbStatement = null;

    /**
     * Constructeur
     *
     * @param url
     * @param user
     * @param password
     */
    public Database() {
        this.dbURL = "localhost/RMI";
        this.user = "root";
        this.password = "p@ssw0rd";
    }

    public Database(String url, String user, String password) {
        this.dbURL = url;
        this.user = user;
        this.password = password;
    }

    /**
     * Connecter à la base de donnée
     *
     * @return false en cas d'échec
     */
    public Boolean connect() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            this.dbConnect = DriverManager.getConnection("jdbc:mysql://" + this.dbURL, this.user, this.password);
            this.dbStatement = this.dbConnect.createStatement();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    /**
     * Executer une requete SQL
     *
     * @param sql
     * @return resultat de la requete
     */
    public ResultSet exec(String sql) {
        try {
            System.out.println("Requete SQL = " + sql);
            ResultSet rs = this.dbStatement.executeQuery(sql);
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public int execUpdate(String sql) {
        try {
            System.out.println("Requete SQL Update = " + sql);
            int rs = this.dbStatement.executeUpdate(sql);
            System.out.println("finsh ");
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    /**
     * Fermer la connexion au serveur de DB
     */
    public void close() {
        try {
            this.dbStatement.close();
            this.dbConnect.close();
            this.dbConnect.close();
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void saveObject(String host, Object javaObject) throws Exception {
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://" + this.dbURL, this.user, this.password);
            PreparedStatement ps = null;
            String sql = null;

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);

            oos.writeObject(javaObject);
            oos.flush();
            oos.close();
            bos.close();

            byte[] data = bos.toByteArray();

            sql = "INSERT INTO RMIHISTO (ip_serveur, method_name, method_value) VALUES(?,?,?)";
            //insert into javaobject (javaObject) values(?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, host);
            ps.setString(2, "Processus");
            ps.setObject(3, data);
            ps.executeUpdate();
            ps.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public List<Proc> getObject(String id) throws Exception {
        Object rmObj = null;
        Connection conn = DriverManager.getConnection("jdbc:mysql://" + this.dbURL, this.user, this.password);
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = null;
        List<Proc> liste_proc = null;

        sql = "SELECT * FROM RMIHISTO WHERE id_histo='"+id+"'";

        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();

        if (rs.next()) {
            ByteArrayInputStream bais;

            ObjectInputStream ins;

            try {

                bais = new ByteArrayInputStream(rs.getBytes("method_value"));

                ins = new ObjectInputStream(bais);

                liste_proc = (List<Proc>) ins.readObject();

                ins.close();

            } catch (Exception e) {

                e.printStackTrace();
            }

        }
        
        ps.close();
        conn.close();

        return liste_proc;
    }
}
